package com.caishi.lkx.distribution.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caishi.lkx.common.context.UserContext;
import com.caishi.lkx.distribution.model.DistributionCommissionRecords;
import com.caishi.lkx.distribution.model.DistributionWithdrawalSummary;
import com.caishi.lkx.distribution.service.ICommissionRecordsService;
import com.caishi.lkx.distribution.service.IDistributionWithdrawalSummaryService;
import com.zzw.common.annotation.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/distribute/commission")
public class CommissionController {
    private final ICommissionRecordsService commissionRecordsService;
    private final IDistributionWithdrawalSummaryService distributionWithdrawalSummaryService;
    private final UserContext userContext;

    public CommissionController(ICommissionRecordsService commissionRecordsService, UserContext userContext,IDistributionWithdrawalSummaryService distributionWithdrawalSummaryService) {
        this.commissionRecordsService = commissionRecordsService;
        this.userContext = userContext;
        this.distributionWithdrawalSummaryService = distributionWithdrawalSummaryService;
    }

    // 获取已支付分销订单对应的分佣表，分页
    // 筛选条件有时间范围，分销员，订单编号
    @GetMapping("/list")
    @ApiResult
    public Page<DistributionCommissionRecords> list(@RequestParam Map<String, Object> params){
        QueryWrapper<DistributionCommissionRecords> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",false);
        if(params.get("startTime") != null&& params.get("endTime") != null&& params.get("startTime") != ""&& params.get("endTime") != ""){
            queryWrapper.between("created_at",params.get("startTime"),params.get("endTime"));
        }
        if(params.get("userId") != null&& params.get("userId") != ""){
            queryWrapper.eq("user_id",params.get("userId"));
        }
        if(params.get("orderId") != null&& params.get("orderId") != ""){
            queryWrapper.eq("order_id",params.get("orderId"));
        }
        int current = Integer.parseInt(params.get("current").toString());
        int size = Integer.parseInt(params.get("size").toString());
        return commissionRecordsService.page(new Page<DistributionCommissionRecords>(current, size),queryWrapper);
    }
    // 获取某人的分佣详情，包括可提现金额与已提现金额
    @GetMapping("/amount")
    @ApiResult
    public DistributionWithdrawalSummary getDistributorTransferInfo(){
        String userId = userContext.currentUserId();
        return distributionWithdrawalSummaryService.getById(userId);
    }
}
